<?php 

session_start();

$admin = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stiphpcms Setup</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: small;
	color: #041F33;
}
body {
	background-color: #E1E1E1;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	text-align: center;
}
a:link {
	color: #D41F00;
}
a:visited {
	color: #D41F00;
}
a:hover {
	color: #FFBF55;
}
#form1 #warn strong {
	color: #D41F00;
}
-->
</style></head>

<body>
<h1>Stiphpcms 0.0.1</h1>
<p>Installation file</p>
<p>&nbsp;</p>

<form id="form1" name="form1" method="post" action="install.php?t=create">
  <p>
    <label for="sitename">Site name : </label>
    <input type="text" name="sitename" id="sitename" tabindex="10" />
  </p>
  <h2>Admin account </h2>
  <p>
    <label for="username">Username : </label>
    <input type="text" name="username" id="username" tabindex="20" />
  </p>
    <p>Password : </label>
    <input type="password" name="password" id="password" tabindex="30" />
  </p>
  <p>
    <label for="password2">Password (Confirmation) : </label>
    <input type="password" name="password2" id="password2" tabindex="40" />
  </p>
    <p>
    <label for="email">Email : </label>
    <input type="text" name="email" id="email" tabindex="50" />
  </p>
    <p>
      <label for="button"></label>
      <input type="submit" name="button" id="button" value="Create" />
    </p>
</form>
<p>&nbsp;</p>
</body>
</html>';

$sql_config = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stiphpcms Setup</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: small;
	color: #041F33;
}
body {
	background-color: #E1E1E1;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	text-align: center;
}
a:link {
	color: #D41F00;
}
a:visited {
	color: #D41F00;
}
a:hover {
	color: #FFBF55;
}
#form1 #warn strong {
	color: #D41F00;
}
-->
</style></head>

<body>
<h1>Stiphpcms 0.0.1</h1>
<p>Installation file</p>
<p>&nbsp;</p>
<h2>MySQL Configuration</h2>
<form id="form1" name="form1" method="post" action="install.php?t=sql">
  <p>
    <label for="hostname">Hostname : </label>
    <input name="hostname" type="text" id="hostname" tabindex="10" value="localhost" />
  </p>
    <p>
    <label for="username">Username : </label>
    <input type="text" name="username" id="username" tabindex="20" />
  </p>
    <p>
    <label for="password">Password : </label>
    <input type="password" name="password" id="password" tabindex="30" />
  </p>
    <p>
    <label for="database">Database : </label>
    <input type="text" name="database" id="database" tabindex="40" />
  </p>
  <p>
    <label for="prefix">Table prefix : </label>
    <input name="prefix" type="text" id="prefix" tabindex="50" value="spc_" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Start setup" />
  </p>
</form>
<p>&nbsp;</p>
</body>
</html>';

function sql_query($query,$prefix)
{
	$query =  str_replace('%prefix%',$prefix,$query);
	mysql_query($query);
}

function error($msg)
{
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stiphpcms Setup</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: small;
	color: #041F33;
}
body {
	background-color: #E1E1E1;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	text-align: center;
}
a:link {
	color: #D41F00;
}
a:visited {
	color: #D41F00;
}
a:hover {
	color: #FFBF55;
}
#form1 #warn strong {
	color: #D41F00;
}
-->
</style></head>

<body>
<h1>Stiphpcms 0.0.1</h1>
<p>Installation file</p>
<p>&nbsp;</p>
<h2>'.$msg.'</h2>
<p>&nbsp;</p>
</body>
</html>
	';
	exit;
}

if(isset($_GET['t']))
{
	$task = $_GET['t'];
	
	if($task=='dosql')
	{
		if(!is_writable('core/config.php'))
		{
			error('*** "core/config.php" must be writable (chmod 077) to continue the setup ! ***');
		}
		
		echo $sql_config;
	}
	else if($task=='sql')
	{
		$sql_server = $_POST['hostname'];
		$sql_user = $_POST['username'];
		$sql_pass = $_POST['password'];
		$sql_db = $_POST['database'];
		$sql_prefix = $_POST['prefix'];
		
		if(!mysql_connect($sql_server,$sql_user,$sql_pass))
		{
			error('Can\'t make the connection with the MySQL database, review your hostname,username and password');
		}
		
		if(!mysql_select_db($sql_db))
		{
			error('Can\'t select the database, review the database field');
		}
		
		$_SESSION['sql_server'] = $sql_server;
		$_SESSION['sql_user'] = $sql_user;
		$_SESSION['sql_pass'] = $sql_pass;
		$_SESSION['sql_db'] = $sql_db;
		$_SESSION['sql_prefix'] = $sql_prefix;
		
		require_once("sql/sql_struct.php");
		
		foreach($struct as $key => $query)
		{
			sql_query($query,$sql_prefix);
		}
		
		echo $admin;
		
	}
	else if($task=='create')
	{
		$sql_server = $_SESSION['sql_server'];
		$sql_user = $_SESSION['sql_user'];
		$sql_pass = $_SESSION['sql_pass'];
		$sql_db = $_SESSION['sql_db'];
		$sql_prefix = $_SESSION['sql_prefix'];
		
		require_once("core/tools/string.php");

		if(!mysql_connect($sql_server,$sql_user,$sql_pass))
		{
			error('Can\'t make the connection with the MySQL database, review your hostname,username and password');
		}
		
		if(!mysql_select_db($sql_db))
		{
			error('Can\'t select the database, review the database field');
		}
		
		$_POST = array_sql_escape($_POST);
		
		$sitename = $_POST['sitename'];
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$email = $_POST['email'];
		$stamp = time();
		
		if($_POST['password']!=$_POST['password2'])
		{
			error('The two password aren\'t the same!');
		}
		
		sql_query("INSERT INTO %prefix%users VALUES('','$username','$password','$email',$stamp,1024,'NO_ACTIVATION_CODE')",$sql_prefix);
		
		sql_query("INSERT INTO %prefix%profiles SET user_id=1,birthdate='-2145898800'",$sql_prefix);
		
		$config_file = '<?php

$cms_config = new Config();

$cms_config->sql_server = \''.$sql_server.'\';
$cms_config->sql_user = \''.$sql_user.'\';
$cms_config->sql_pass = \''.$sql_pass.'\';
$cms_config->sql_db = \''.$sql_db.'\';
$cms_config->sql_prefix = \''.$sql_prefix.'\';

$cms_config->sitename = \''.$sitename.'\';

$cms_config->authkey = md5($cms_config->sitename.\'#MYKEY102937#\');

$cms_config->ssl_mode = false;

$cms_config->use_email_activation = false;
$cms_config->use_pearmail = false; //true for pear and false for classic mail()
?>';
		$fp = fopen("core/config.php",'w');
		fwrite($fp,$config_file);
		fclose($fp);
		
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stiphpcms Setup</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: small;
	color: #041F33;
}
body {
	background-color: #E1E1E1;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	text-align: center;
}
a:link {
	color: #D41F00;
}
a:visited {
	color: #D41F00;
}
a:hover {
	color: #FFBF55;
}
#form1 #warn strong {
	color: #D41F00;
}
-->
</style></head>

<body>
<h1>Stiphpcms 0.0.1</h1>
<p>Installation file</p>
<p>&nbsp;</p>
<p>The setup process is now finish ! </p>
<p>You can do the final step by loading the <a href="index.php">index.php</a> file</p>
<p id="warn"><strong>** ITS VERY IMPORTANT TO DELETE install.php AFTER THE SETUP !</strong></p>
<h2>&nbsp;</h2>
<p>&nbsp;</p>
</body>
</html>';
		
		
	}
	
}
else
{
	$gpl = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stiphpcms Setup</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: small;
	color: #041F33;
}
body {
	background-color: #E1E1E1;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	text-align: center;
}
a:link {
	color: #D41F00;
}
a:visited {
	color: #D41F00;
}
a:hover {
	color: #FFBF55;
}
#form1 #warn strong {
	color: #D41F00;
}
-->
</style></head>

<body>
<h1>Stiphpcms : 0.0.1</h1>
<p>Installation file</p>
<p>&nbsp;</p>
<h2>License</h2>
<form id="form1" name="form1" method="post" action="install.php?t=dosql">
  <p>
    <textarea name="gpl" id="gpl" cols="80" rows="30">';

$fp = fopen('doc/gplv2.txt','r');

while($line = fgets($fp))
{
	$gpl .= htmlentities($line);
}

fclose($fp);

  $gpl .= '</textarea>
  </p>
  <p>
    <label for="submit"></label>
    <input type="submit" name="submit" id="submit" value="I Agree" />
  </p>
</form>
<p>&nbsp;</p>
</body>
</html>';
	
  echo $gpl;
}

?>